package cn.ydxiaoshuai.tools.util;

import cn.ydxiaoshuai.tools.vo.WXAccessToken;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

/**
 * @author 小帅丶
 * @className ImageCheckUtil
 * @Description 有图片安全检查
 * @Date 2020/10/13-18:48
 **/
@Slf4j
public class ImageCheckUtil {
    private static Integer IMG_WIDTH = 750;
    private static Integer IMG_HEIGHT = 1334;
    /**
     * 图片检测接口
     */
    private static String IMG_SEC_URL = "https://api.weixin.qq.com/wxa/img_sec_check?access_token=";
    /**
     * @Author 小帅丶
     * @Description 图片安全检查
     * @Date  2020/9/29
     * @param image 图片byte
     * @param access_token 微信的token
     * @return cn.ydxiaoshuai.modules.weixin.po.WXAccessToken
     **/
    public static WXAccessToken checkImg(byte[] image,String access_token) throws Exception{
        WXAccessToken bean;
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(image);
        BufferedImage bufferedImage = ImageIO.read(byteArrayInputStream);;
        String url = IMG_SEC_URL + access_token;
        return checkImgMethod(url,bufferedImage);
    }
    /**
     * @Author 小帅丶
     * @Description 图片安全检查
     * @Date  2020/9/29
     * @param imageUrl 图片公网地址
     * @param access_token 微信的token
     * @return cn.ydxiaoshuai.modules.weixin.po.WXAccessToken
     **/
    public static WXAccessToken checkImg(String imageUrl,String access_token) throws Exception{
        WXAccessToken bean;
        BufferedImage bufferedImage = MergeImageUtil.imgUrlConvertBufferedImage(imageUrl);
        String url = IMG_SEC_URL + access_token;
        return checkImgMethod(url,bufferedImage);
    }
    /**
     * @Author 小帅丶
     * @Description 公共代码提取
     * @Date  2020/10/14 10:23
     * @param url 接口地址
     * @param bufferedImage 要检测的图片
     * @return cn.ydxiaoshuai.tools.vo.WXAccessToken
     **/
    private static WXAccessToken checkImgMethod(String url,BufferedImage bufferedImage) throws Exception{
        WXAccessToken bean;
        //750px * 1334px
        if (bufferedImage.getWidth() > IMG_WIDTH && bufferedImage.getHeight() > IMG_HEIGHT) {
            //缩放图片
            byte[] newImage = MergeImageUtil.zoomImageByte(bufferedImage);
            String result = ImgCheckHttpUtil.uploadFile(url,newImage);
            log.info(result);
            bean = JSON.parseObject(result, WXAccessToken.class);
            log.info("general缩放图片检测结果 = {}",result);
        } else {
            ByteArrayOutputStream outputStreamSource = new ByteArrayOutputStream();
            ImageIO.write(bufferedImage, "jpg", outputStreamSource);
            String result = ImgCheckHttpUtil.uploadFile(url,outputStreamSource.toByteArray());
            log.info(result);
            bean = JSON.parseObject(result, WXAccessToken.class);
            log.info("general图片检测结果 = {}",result);
        }
        return bean;
    }
}
